package com.leyou.auth.service.impl;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.auth.entity.ClientInfo;
import com.leyou.auth.mapper.ClientInfoMapper;
import com.leyou.auth.service.ClientAuthService;
import com.leyou.common.exception.LyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @description:
 * @author: YHL
 * @time: 2021/3/6 20:51
 */
@Service
public class ClientAuthServiceImpl extends ServiceImpl<ClientInfoMapper, ClientInfo> implements ClientAuthService {
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Value("${ly.jwt.key}")
    private String key;
    @Override
    public String getSecretKey(String clientId, String secret) {
//        根据传入的clientid查询表格client——info 如果查得到 则判断secret，如果没查到直接抛异常
        ClientInfo clientInfo = this.query().eq("client_id", clientId).one();
        if (null==clientInfo|| !passwordEncoder.matches(secret,clientInfo.getSecret())){
            throw new LyException(403,"非法请求");
        }
        return this.key;
    }
}
